﻿<!--
 Copyright 2013 Shawn Henry, Rob Smith, and Michael Friedman

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->
<view:AViewPage 
    x:Class="OneBusAway.WP7.View.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phoneNavigation="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:pivot="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"  
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"   
    xmlns:data="clr-namespace:OneBusAway.WP7.ViewModel;assembly=OneBusAway.WP7.ViewModel" 
    xmlns:view="clr-namespace:OneBusAway.WP7.View;assembly=OneBusAway.WP7.View"
    xmlns:local="clr-namespace:OneBusAway.WP7.View"
    xmlns:m="clr-namespace:Microsoft.Phone.Controls.Maps;assembly=Microsoft.Phone.Controls.Maps" 
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="728"
    xmlns:tilt="clr-namespace:ControlTiltEffect"
    shell:SystemTray.IsVisible="False"
    xmlns:localControls="clr-namespace:Microsoft.Phone.Controls"
    xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
>
    
    <view:AViewPage.Resources>
        <data:MainPageVM x:Key="ViewModel" />

        <DataTemplate x:Key="PushpinTemplate">
            <m:Pushpin PositionOrigin="{Binding location}" />
        </DataTemplate>

        <view:StopRoutesConverter x:Key="StopRoutesConverter"></view:StopRoutesConverter>
        <view:DistanceConverter x:Key="DistanceConverter"></view:DistanceConverter>
        <view:DateTimeDeltaConverter x:Key="DateTimeDeltaConverter"></view:DateTimeDeltaConverter>
        <view:StopDirectionConverter x:Key="StopDirectionConverter"></view:StopDirectionConverter>
        <view:VisibilityConverter x:Key="VisibilityConverter"></view:VisibilityConverter>
        <view:LowercaseConverter x:Key="LowercaseConverter"></view:LowercaseConverter>
        <view:ShortDistanceConverter x:Key="ShortDistanceConverter"></view:ShortDistanceConverter>
        <view:IsFavoriteConverter x:Key="IsFavoriteConverter"></view:IsFavoriteConverter>

        <view:ColorAlphaConverter x:Key="ColorAlphaConverter"/>

        <DataTemplate x:Key="StopTemplate">

            <Grid Margin="5" tilt:TiltEffect.IsTiltEnabled="True">

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="50" />

                    <ColumnDefinition Width="215" />
                    <ColumnDefinition Width="215" />
                </Grid.ColumnDefinitions>

                <Grid.RowDefinitions>
                    <RowDefinition Height="25" />
                    <RowDefinition Height="22" />
                    <RowDefinition Height="22" />
                </Grid.RowDefinitions>

                <Polygon CacheMode="BitmapCache" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="0" Grid.RowSpan="3"  Points="0,0 32,0 32,32 0,61" Fill="{StaticResource OBAForegroundBrush}" Opacity="1.0"/>
                <Canvas CacheMode="BitmapCache" Grid.Row="0" Grid.Column="0" Grid.RowSpan="3"  x:Name="BusIcon" Margin="12">
                    <Canvas.RenderTransform>
                        <TransformGroup>
                            <ScaleTransform ScaleX="0.85" ScaleY="0.85" />
                        </TransformGroup>
                    </Canvas.RenderTransform>
                    <Rectangle Width="26" Height="22" Canvas.Left="1" Canvas.Top="2" Fill="{StaticResource OBABackgroundBrush}" Stroke="{StaticResource OBABackgroundBrush}" StrokeLineJoin="Round" RadiusX="1" RadiusY="1"/>
                    <Ellipse Width="6" Height="6" Canvas.Left="19" Canvas.Top="21" Stretch="Fill" StrokeLineJoin="Round" Stroke="{StaticResource OBABackgroundBrush}" Fill="{StaticResource OBABackgroundBrush}"/>
                    <Ellipse Width="6" Height="6" Canvas.Left="3" Canvas.Top="21" Stretch="Fill" StrokeLineJoin="Round" Stroke="{StaticResource OBABackgroundBrush}" Fill="{StaticResource OBABackgroundBrush}"/>

                    <Rectangle Width="22" Height="10" Canvas.Left="3" Canvas.Top="7" Fill="{StaticResource OBAForegroundBrush}" Stroke="{StaticResource OBAForegroundBrush}" StrokeLineJoin="Round" RadiusX="1" RadiusY="1"/>
                    <Rectangle Width="6" Height="3" Canvas.Left="3" Canvas.Top="19" Fill="{StaticResource OBAForegroundBrush}" Stroke="{StaticResource OBAForegroundBrush}" StrokeLineJoin="Round" RadiusX="1" RadiusY="1"/>
                    <Rectangle Width="6" Height="3" Canvas.Left="19" Canvas.Top="19" Fill="{StaticResource OBAForegroundBrush}" Stroke="{StaticResource OBAForegroundBrush}" StrokeLineJoin="Round" RadiusX="1" RadiusY="1"/>
                    <Rectangle Width="22" Height="3" Canvas.Left="3" Canvas.Top="3" Fill="{StaticResource OBAForegroundBrush}" Stroke="{StaticResource OBAForegroundBrush}" StrokeLineJoin="Round" RadiusX="1" RadiusY="1"/>

                    <Path Fill="{StaticResource OBABackgroundBrush}" Stretch="Fill" Stroke="{StaticResource OBABackgroundBrush}" StrokeLineJoin="Round" Height="12" Width="1" UseLayoutRounding="False" Canvas.Left="13.5" Canvas.Top="6.5" Data="M16,8 L16,17"/>
                </Canvas>
                <TextBlock Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="0" Text="{Binding name}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource OBAForegroundBrush}" />
                <TextBlock Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="1" Foreground="{StaticResource OBASubtleBrush}" FontSize="{StaticResource PhoneFontSizeSmall}" Text="{Binding Converter={StaticResource StopRoutesConverter}}" />
                <TextBlock Grid.Column="1" Grid.Row="2" Foreground="{StaticResource OBASubtleBrush}" FontSize="{StaticResource PhoneFontSizeSmall}" Text="{Binding Converter={StaticResource StopDirectionConverter}}" />
                <TextBlock Grid.Column="2" Grid.Row="2" Foreground="{StaticResource OBASubtleBrush}" FontSize="{StaticResource PhoneFontSizeSmall}" Text="{Binding Converter={StaticResource DistanceConverter}, ConverterParameter={StaticResource ViewModel}}" />
            </Grid>
        </DataTemplate>

        <DataTemplate x:Key="RouteTemplate">
            <Grid Margin="0,0,0,20" Height="106" Width="480">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="90" />
                    <ColumnDefinition Width="390" />
                </Grid.ColumnDefinitions>

                <StackPanel Grid.Column="0" VerticalAlignment="Center" Margin="10,0,0,0" Orientation="Vertical" Height="90">
                    <TextBlock Text="{Binding Path=Route.shortName}" FontSize="{StaticResource PhoneFontSizeExtraLarge}"  Foreground="{StaticResource OBAForegroundBrush}" />
                    <TextBlock Foreground="{StaticResource OBASubtleBrush}"  Margin="2,0,0,0" FontSize="{StaticResource PhoneFontSizeSmall}" Text="{Binding Path=Route, Converter={StaticResource ShortDistanceConverter}, ConverterParameter={StaticResource ViewModel}}" />
                </StackPanel>
                <StackPanel Grid.Column="1" VerticalAlignment="Center">
                    <ItemsControl x:Name="RouteDirectionsItemControl" ItemsSource="{Binding Path=RouteStops}" >
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <StackPanel Width="390" Height="106"/>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <ListBoxItem x:Name="DirectionListBoxItem"
                                             Content="{Binding Path=name, Converter={StaticResource LowercaseConverter}}"
                                             FontSize="{StaticResource PhoneFontSizeLarge}"
                                             Foreground="{StaticResource OBAForegroundBrush}"
                                             Margin="0,5,0,5"
                                             Width="390"
                                             Height="45"
                                             tilt:TiltEffect.IsTiltEnabled="True"
                                             >
                                    <toolkit:GestureService.GestureListener>
                                        <toolkit:GestureListener Tap="RouteDirection_Tap"/>
                                    </toolkit:GestureService.GestureListener>
                                </ListBoxItem>
                                
                            </DataTemplate>

                        </ItemsControl.ItemTemplate>
                    </ItemsControl>

                </StackPanel>
            </Grid>
        </DataTemplate>

        <DataTemplate x:Key="FavTemplate">
            <Grid Margin="5,0,5,20" tilt:TiltEffect.IsTiltEnabled="True">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="80" />
                    <ColumnDefinition Width="200" />
                    <ColumnDefinition Width="200" />
                </Grid.ColumnDefinitions>

                <!-- TODO convert to fixed dimensions for better perf.  
                I'm not sure what a good set of dimensions for this is.
                Needs some experimenting.
                -->
                <Grid.RowDefinitions>
                    <RowDefinition Height="1*" />
                    <RowDefinition Height="1*" />
                    <RowDefinition Height="1*" />
                </Grid.RowDefinitions>

                <Polygon Visibility="{Binding Converter={StaticResource IsFavoriteConverter}}" CacheMode="BitmapCache" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="8,4,0,0" Grid.Column="0" Grid.RowSpan="3"  Points="0,0 32,0 32,32 0,61" Fill="{StaticResource OBAForegroundBrush}" Opacity="1.0"/>
                <Canvas Visibility="{Binding Converter={StaticResource IsFavoriteConverter}}" CacheMode="BitmapCache" Grid.Row="0" Grid.Column="0" Grid.RowSpan="3"  x:Name="BusIcon" Margin="12,8,0,0">
                    <Canvas.RenderTransform>
                        <TransformGroup>
                            <ScaleTransform ScaleX="0.85" ScaleY="0.85" />
                        </TransformGroup>
                    </Canvas.RenderTransform>
                    <Rectangle Width="26" Height="22" Canvas.Left="1" Canvas.Top="2" Fill="{StaticResource OBABackgroundBrush}" Stroke="{StaticResource OBABackgroundBrush}" StrokeLineJoin="Round" RadiusX="1" RadiusY="1"/>
                    <Ellipse Width="6" Height="6" Canvas.Left="19" Canvas.Top="21" Stretch="Fill" StrokeLineJoin="Round" Stroke="{StaticResource OBABackgroundBrush}" Fill="{StaticResource OBABackgroundBrush}"/>
                    <Ellipse Width="6" Height="6" Canvas.Left="3" Canvas.Top="21" Stretch="Fill" StrokeLineJoin="Round" Stroke="{StaticResource OBABackgroundBrush}" Fill="{StaticResource OBABackgroundBrush}"/>

                    <Rectangle Width="22" Height="10" Canvas.Left="3" Canvas.Top="7" Fill="{StaticResource OBAForegroundBrush}" Stroke="{StaticResource OBAForegroundBrush}" StrokeLineJoin="Round" RadiusX="1" RadiusY="1"/>
                    <Rectangle Width="6" Height="3" Canvas.Left="3" Canvas.Top="19" Fill="{StaticResource OBAForegroundBrush}" Stroke="{StaticResource OBAForegroundBrush}" StrokeLineJoin="Round" RadiusX="1" RadiusY="1"/>
                    <Rectangle Width="6" Height="3" Canvas.Left="19" Canvas.Top="19" Fill="{StaticResource OBAForegroundBrush}" Stroke="{StaticResource OBAForegroundBrush}" StrokeLineJoin="Round" RadiusX="1" RadiusY="1"/>
                    <Rectangle Width="22" Height="3" Canvas.Left="3" Canvas.Top="3" Fill="{StaticResource OBAForegroundBrush}" Stroke="{StaticResource OBAForegroundBrush}" StrokeLineJoin="Round" RadiusX="1" RadiusY="1"/>

                    <Path Fill="{StaticResource OBABackgroundBrush}" Stretch="Fill" Stroke="{StaticResource OBABackgroundBrush}" StrokeLineJoin="Round" Height="12" Width="1" UseLayoutRounding="False" Canvas.Left="13.5" Canvas.Top="6.5" Data="M16,8 L16,17"/>
                </Canvas>

                <TextBlock Grid.Column="0" Grid.Row="0" Grid.RowSpan="3" VerticalAlignment="Center" Text="{Binding Path=route.shortName}" FontSize="{StaticResource PhoneFontSizeLarge}" Margin="5"  Foreground="{StaticResource OBAForegroundBrush}" />
                <TextBlock Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="0" Text="{Binding Path=Title}"  FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource OBAForegroundBrush}" />
                <TextBlock Grid.Column="1" Grid.Row="1" Foreground="{StaticResource OBASubtleBrush}" FontSize="{StaticResource PhoneFontSizeSmall}" Text="{Binding Path=stop, Converter={StaticResource DistanceConverter}, ConverterParameter={StaticResource ViewModel}}" />
                <TextBlock Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="2" Text="{Binding  Path=Detail}"  FontSize="{StaticResource PhoneFontSizeNormal}"  Foreground="{StaticResource OBAForegroundBrush}" />
            </Grid>
        </DataTemplate>

        <Storyboard x:Name="SearchStoryboard">
            <DoubleAnimation Duration="0:0:0.2" To="0" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateY)" Storyboard.TargetName="SearchPanel" d:IsOptimized="True"/>
            <DoubleAnimation Duration="0:0:0.2" To="1.0" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="SearchPanel" d:IsOptimized="True"/>
        </Storyboard>
                       
    </view:AViewPage.Resources>

    <Grid x:Name="LayoutRoot" Background="{StaticResource OBAPrimaryBrush}">
        
        <pivot:Pivot Name="PC" Foreground="{StaticResource OBAForegroundBrush}" SelectionChanged="PC_SelectionChanged">
            <pivot:PivotItem Header="routes">
                <ListBox 
                    x:Name="RoutesListBox" 
                    ItemsSource="{Binding Path=DisplayRouteForLocation.Current}" 
                    ItemTemplate="{StaticResource RouteTemplate}"
                    >
                    <!--Removed DesignData from ListBox because of conflict with GestureListener-->
                    <!--d:DataContext="{d:DesignData Source=SampleData/MainViewModelSampleData.xaml}"-->
                </ListBox>
            </pivot:PivotItem>
            <pivot:PivotItem Header="stops">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <!--<Image Grid.Row="0" Source="/Images/appbar.refresh.rest.png" />-->
                    <Button 
                        Grid.Row="0" 
                        x:Name="stopsMapBtn" 
                        Click="stopsMapBtn_Click" 
                        BorderBrush="Transparent" 
                        BorderThickness="0" 
                        HorizontalContentAlignment="Stretch" 
                        VerticalContentAlignment="Stretch" 
                        Padding="0"
                        >
                        <m:Map x:Name="StopsMap" CopyrightVisibility="Collapsed" LogoVisibility="Collapsed" ScaleVisibility="Collapsed" CredentialsProvider="{StaticResource MapCredentials}"
                               Center="{Binding Path=LocationTracker.CurrentLocationSafe, Source={StaticResource ViewModel}}"  ZoomLevel="17" IsEnabled="False">
                            <m:MapLayer x:Name="BusStopsLayer">
                                <m:MapItemsControl x:Name="StopsMapItemsControl" ItemsSource="{Binding Path=StopsForLocation, Source={StaticResource ViewModel}}">
                                    <m:MapItemsControl.ItemTemplate>
                                        <DataTemplate>
                                            <m:Pushpin Location="{Binding location}" Style="{StaticResource BusStopSmallPushpinStyle}" PositionOrigin="Center" />
                                        </DataTemplate>
                                    </m:MapItemsControl.ItemTemplate>
                                </m:MapItemsControl>
                            </m:MapLayer>
                            <m:MapLayer x:Name="MyLocationLayer">
                                <m:MapLayer.Children>
                                    <m:Pushpin Location="{Binding Path=LocationTracker.CurrentLocationSafe, Source={StaticResource ViewModel}}" PositionOrigin="Center" Style="{StaticResource MyLocationPushpinStyle}" />
                                </m:MapLayer.Children>
                            </m:MapLayer>
                        </m:Map>
                    </Button>
                    <ListBox Grid.Row="1"
                    x:Name="StopsListBox"
                    d:DataContext="{d:DesignData Source=SampleData/MainViewModelSampleData.xaml}"
                    ItemsSource="{Binding Path=StopsForLocation}" 
                    ItemTemplate="{StaticResource StopTemplate}" 
                    SelectionChanged="StopsListBox_SelectionChanged"
                    />
                </Grid>
                
            </pivot:PivotItem>
            <pivot:PivotItem Header="recent">
                <ListBox 
                    x:Name="RecentsListBox" 
                    ItemsSource="{Binding Path=Recents}" 
                    d:DataContext="{d:DesignData Source=SampleData/MainViewModelSampleData.xaml}"
                    ItemTemplate="{StaticResource FavTemplate}" 
                    SelectionChanged="RecentsListBox_SelectionChanged" 
                    />
            </pivot:PivotItem>
            <pivot:PivotItem Header="favorites">
                <ListBox 
                    x:Name="FavoritesListBox"
                    d:DataContext="{d:DesignData Source=SampleData/MainViewModelSampleData.xaml}"
                    ItemsSource="{Binding Path=Favorites}" 
                    ItemTemplate="{StaticResource FavTemplate}" 
                    SelectionChanged="FavoritesListBox_SelectionChanged"
                    />
            </pivot:PivotItem>
        </pivot:Pivot>

        <Rectangle
            Height="32"
            Fill="{StaticResource OBADarkBrush}"
            VerticalAlignment="Top"
            Visibility="{Binding Path=operationTracker.Loading, Source={StaticResource ViewModel}, Converter={StaticResource VisibilityConverter}}"
            />

        <TextBlock 
            Visibility="{Binding Path=operationTracker.Loading, Source={StaticResource ViewModel}, Converter={StaticResource VisibilityConverter}}"
            Text="{Binding Path=operationTracker.LoadingMessage, Source={StaticResource ViewModel}}"
            Foreground="{StaticResource OBAForegroundBrush}"
            VerticalAlignment="Top"
            FontSize="20"
            Margin="23,0,0,0"
            />

        <StackPanel 
            x:Name="SearchPanel" 
            Opacity="0" 
            Background="{Binding Source={StaticResource OBADarkBrush}, Converter={StaticResource ColorAlphaConverter}, ConverterParameter={StaticResource OBABackgroundBrush}}" 
            Height="80" 
            VerticalAlignment="Top" 
            RenderTransformOrigin="0.5,0.5"
            >
            <StackPanel.RenderTransform>
                <CompositeTransform TranslateY="-75"/>
            </StackPanel.RenderTransform>
            <TextBox x:Name="SearchInputBox" LostFocus="SearchInputBox_LostFocus" KeyUp="SearchInputBox_KeyUp" InputScope="CurrencyAmountAndSymbol" />
            <TextBlock 
				Text="By route number, stop number, location, or address"  
				Foreground="{StaticResource OBAForegroundBrush}" FontSize="16" HorizontalAlignment="Center">
				<TextBlock.RenderTransform>
				<CompositeTransform TranslateY="-14"/>
			</TextBlock.RenderTransform>
            </TextBlock>
        </StackPanel>

        <localControls:PerformanceProgressBar
            VerticalAlignment="Top"
            IsLoading="{Binding Path=operationTracker.Loading, Source={StaticResource ViewModel}}"
            Foreground="{StaticResource OBALightBrush}"
            Style="{StaticResource PerformanceProgressBarStyle}"
            />
    </Grid>

    <view:AViewPage.ApplicationBar>
        <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
            <shell:ApplicationBarIconButton x:Name="appbar_center" IconUri="/Images/appbar.refresh.rest.png" Text="Refresh" Click="appbar_refresh_Click"></shell:ApplicationBarIconButton>
            <shell:ApplicationBarIconButton x:Name="appbar_search" IconUri="/Images/appbar.feature.search.rest.png" Text="Search" Click="appbar_search_Click"></shell:ApplicationBarIconButton>
            <shell:ApplicationBar.MenuItems>
                <shell:ApplicationBarMenuItem x:Name="appbar_settings" Click="appbar_settings_Click" Text="settings"></shell:ApplicationBarMenuItem>
                <shell:ApplicationBarMenuItem x:Name="appbar_about" Click="appbar_about_Click" Text="about"></shell:ApplicationBarMenuItem>
            </shell:ApplicationBar.MenuItems>
        </shell:ApplicationBar>
    </view:AViewPage.ApplicationBar>
</view:AViewPage>
